package com.athena.view 
{
	import com.athena.ui.setTable;
	import com.athena.utils.Util;
	import com.cubeia.firebase.io.protocol.CreateTableRequestPacket;
	import com.cubeia.firebase.io.protocol.Param;
	import com.cubeia.firebase.util.ParameterUtil;
	import fl.containers.BaseScrollPane;
	import fl.controls.RadioButton;
	import fl.controls.RadioButtonGroup;
	import flash.display.Shape;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	/**
	 * ...
	 * @author ...
	 */
	public class settingTable extends setTable
	{
		private var arrMark : Array = [20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000];
		private var type : int = 0;
		private var rbg : RadioButtonGroup;
		public function settingTable(_name : String = "Vui là chính", _m : Number = 0, _seat : int = 0, _agMin : Number = 0, _vipMin : int = 0, _rule: int = 3) 
		{
			var glow:GlowFilter = new GlowFilter();
			glow.color = 0x000000;
			glow.alpha = 0.5;
			glow.blurX = 10;
			glow.blurY = 10;
			glow.quality = BitmapFilterQuality.MEDIUM;

			filters=[glow];
			
			var _w : Number = width; var _h : Number = height;
			var rectangle:Shape = new Shape();
			rectangle.graphics.beginFill(0x000000, 1);
			rectangle.graphics.drawRect((_w - Main.getInstance().config.demension.x) / 2, (_h - Main.getInstance().config.demension.y) / 2, Main.getInstance().config.demension.x, Main.getInstance().config.demension.x);
			rectangle.alpha = 0.2;
			rectangle.graphics.endFill();
			addChild(rectangle);
			setChildIndex(rectangle, 0);
			
			this.x = (Main.getInstance().config.demension.x - _w) / 2;
			this.y = (Main.getInstance().config.demension.y - _h) / 2;
			
			
			title_txt.text = (_m == 0) ? "TẠO BÀN" : "CÀI ĐẶT BÀN CHƠI";
			type = (_m == 0) ? 0 : 1;
			
			addEvent();
			
			rbg = new RadioButtonGroup("groupMark");
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;
			formatText.color = 0xFFFFFF ;
			formatText.size = 12 ;
			for(var i : int = 1; i <= arrMark.length; i++) {
				var rb : RadioButton = new RadioButton;
				if (i == 1 && type == 0) rb.selected = true;
				if (type == 1 && arrMark[i - 1] == _m) rb.selected = true;
				rb.x = ((i - 1) % 2 == 0) ? 39 : 178;
				rb.y = 45 + Math.floor((i - 1) / 2) * 36;
				rb.width = 120;
				
				rb.setStyle("textFormat",formatText);
				rb.group = rbg;
				rb.label = Util.numberFormat(arrMark[i - 1], 0) + Main.getInstance().gameObject.UnitMark;
				rb.value = arrMark[i - 1];
				addChild(rb);
			}
		}
		private function addEvent() : void {
			ok_btn.addEventListener(MouseEvent.CLICK, accept);
			close_btn.addEventListener(MouseEvent.CLICK, close);
			//addEventListener(KeyboardEvent.KEY_DOWN, esc);
		}
		private function removeEvent() : void {
			ok_btn.removeEventListener(MouseEvent.CLICK, accept);
			close_btn.removeEventListener(MouseEvent.CLICK, close);
			//removeEventListener(KeyboardEvent.KEY_DOWN, esc);
		}
		private function esc(e : KeyboardEvent) : void {
			trace(e);
			if (e.keyCode == Keyboard.ESCAPE) {
				close(null);
			}
		}
		private function accept(e : MouseEvent)  :void {
			if (rbg.selection == null) return ;
			var m : int = (rbg as Object).selection.value;
			if (Main.getInstance().currPlayer.mark  < m * 10) {
				Util.simpleAlert("Bạn không đủ " + Main.getInstance().gameObject.UnitMark);
				return;
			}
			
			if (type == 0) {
				var requestTablePacket:CreateTableRequestPacket = new CreateTableRequestPacket();
				requestTablePacket.gameid = Main.getInstance().gameObject.GAMEID;
				requestTablePacket.seats = Main.getInstance().config.maxPLayer;
				requestTablePacket.seq = 1;
				requestTablePacket.invitees = new Array();
				var gameid:Param = ParameterUtil.makeIntegerParam("gameId", Main.getInstance().gameObject.GAMEID);
				var name:Param = ParameterUtil.makeStringParam("Name", "vui la chinh");
				var mark:Param = ParameterUtil.makeIntegerParam("Mark", m);
				var amin:Param = ParameterUtil.makeIntegerParam("AG", m * 10);
				var vmin:Param = ParameterUtil.makeIntegerParam("Vip", 0);
				var smax:Param = ParameterUtil.makeIntegerParam("Player", Main.getInstance().config.maxPLayer);
				requestTablePacket.params = [gameid, mark, name, smax, vmin, amin];
				Main.getInstance().socket.send(requestTablePacket);
				Util.showLoading();
			}
			
			close(null);
		}
		
		private function close(e : MouseEvent)  :void {
			removeEvent();
			this.parent.removeChild(this);
		}
		
	}

}